Security News
NIST Misses 2024 Deadline to Clear NVD Backlog
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.
@floating-ui/react-dom
Advanced tools
The @floating-ui/react-dom package is a library for creating floating elements that can be positioned around a target element in a React application. It provides a robust API for positioning tooltips, popovers, dropdowns, and other floating elements, with extensive control over placement, flipping, and shifting based on available space in the viewport.
Positioning Tooltips
This code demonstrates how to create a simple tooltip that appears above a button when clicked. It uses the useFloating hook from @floating-ui/react-dom to manage the tooltip's position and behavior, including automatic adjustment for viewport boundaries.
import { useFloating, offset, flip, shift, arrow } from '@floating-ui/react-dom';
import { useState, useRef } from 'react';
function Tooltip() {
const [open, setOpen] = useState(false);
const arrowRef = useRef(null);
const {x, y, reference, floating, strategy} = useFloating({
placement: 'top',
middleware: [offset(5), flip(), shift({padding: 5}), arrow({element: arrowRef})]
});
return (
<>
<button ref={reference} onClick={() => setOpen(!open)}>
Hover me
</button>
{open && (
<div ref={floating} style={{position: strategy, top: y ?? '', left: x ?? ''}}>
Tooltip content
<div ref={arrowRef} />
</div>
)}
</>
);
}
Creating Popovers
This example shows how to create a popover that appears to the right of a button when clicked. The useFloating hook is used to handle dynamic positioning and flipping to ensure the popover remains visible within the viewport.
import { useFloating, offset, flip, shift } from '@floating-ui/react-dom';
import { useState, useRef } from 'react';
function Popover() {
const [open, setOpen] = useState(false);
const {x, y, reference, floating, strategy} = useFloating({
placement: 'right-start',
middleware: [offset(10), flip(), shift({padding: 8})]
});
return (
<>
<button ref={reference} onClick={() => setOpen(!open)}>
Click me
</button>
{open && (
<div ref={floating} style={{position: strategy, top: y ?? '', left: x ?? ''}}>
Popover content
</div>
)}
</>
);
}
Popper.js is a popular library for managing poppers in web applications. It provides similar functionalities to @floating-ui/react-dom, such as dynamic positioning and flipping of poppers based on the viewport. However, @floating-ui/react-dom is specifically tailored for React and offers a more React-friendly API with hooks.
Tippy.js is another library focused on creating tooltips and popovers. It builds on top of Popper.js and adds an abstraction layer that includes default styling and behavior. Tippy.js is easier to use for simple tooltips and popovers but offers less low-level control compared to @floating-ui/react-dom, which is more configurable and suited for complex positioning scenarios in React applications.
This is the library to use Floating UI with React DOM.
FAQs
Floating UI for React DOM
We found that @floating-ui/react-dom demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.
Security News
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.